package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.basic.context.ContextUtil;
import top.tangyh.basic.exception.BizException;
import top.tangyh.lamp.care.mapper.BCareTemplateMapper;
import top.tangyh.lamp.care.service.BCareTemplateService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BCareTemplateManager;
import top.tangyh.lamp.care.entity.BCareTemplate;
import top.tangyh.lamp.care.vo.save.BCareTemplateSaveVO;
import top.tangyh.lamp.care.vo.update.BCareTemplateUpdateVO;
import top.tangyh.lamp.care.vo.result.BCareTemplateResultVO;
import top.tangyh.lamp.care.vo.query.BCareTemplatePageQuery;
import top.tangyh.lamp.common.utils.ConvertUtil;
import top.tangyh.lamp.common.utils.SecurityUtil;

import java.util.List;

/**
 * <p>
 * 业务实现类
 * 护理记录模板表
 * </p>
 *
 * @author zqb
 * @date 2024-08-02 10:21:06
 * @create [2024-08-02 10:21:06] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BCareTemplateServiceImpl extends SuperServiceImpl<BCareTemplateManager, Long, BCareTemplate> implements BCareTemplateService {


    private final BCareTemplateMapper bCareTemplateMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addOrUpdateCareTemplate(BCareTemplateSaveVO careTemplatePO) {
        if (null == careTemplatePO.getId()) {
            BCareTemplate po= ConvertUtil.convert(careTemplatePO,BCareTemplate.class);
            bCareTemplateMapper.insert(po);
            Long id = po.getId();
            return id;
        } else {
//            YxCareTemplatePO old = yxCareTemplateMapper.selectById(yxCareTemplatePO.getId());
//            if(null != SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy())){
//                throw new BizException(SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy()));
//            }
            BCareTemplate old = bCareTemplateMapper.selectById(careTemplatePO.getId());
            if (null != SecurityUtil.checkRecordAuth(old.getCreatedBy(), old.getUpdatedBy(), ContextUtil.getUserId())) {
                throw new BizException(SecurityUtil.checkRecordAuth(old.getCreatedBy(), old.getUpdatedBy(), ContextUtil.getUserId()));
            }
            BCareTemplate po= ConvertUtil.convert(careTemplatePO,BCareTemplate.class);
            int result = bCareTemplateMapper.updateById(po);
            return po.getId();
        }
    }

    @Override
    public  List<BCareTemplateResultVO> selectCareTemplateByPid(String patientId, Integer type) {

        LambdaQueryWrapper<BCareTemplate> wrapper = new QueryWrapper<BCareTemplate>()
                .lambda().eq(BCareTemplate::getPatientIdent, patientId)
                .eq(BCareTemplate::getType, type);
        List<BCareTemplate> result = this.list(wrapper);

        List<BCareTemplateResultVO> listVo=ConvertUtil.convertList(result,BCareTemplateResultVO.class);

        return listVo;
    }

    @Override
    public List<BCareTemplateResultVO> listAllByType(Integer type) {
        LambdaQueryWrapper<BCareTemplate> wrapper = new QueryWrapper<BCareTemplate>()
                .lambda().eq(BCareTemplate::getType, type)
                .eq(BCareTemplate::getIsDelete,0);
        List<BCareTemplate> result = this.list(wrapper);

        List<BCareTemplateResultVO> listVo=ConvertUtil.convertList(result,BCareTemplateResultVO.class);
        return  listVo;
    }
}


